package com.freak.wanandroid.model.login.register;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.freak.wanandroid.R;
import com.freak.wanandroid.base.fragment.BaseLifecycleFragment;
import com.freak.wanandroid.bean.LoginBean;
import com.freak.wanandroid.model.login.LoginActivity;
import com.freak.wanandroid.util.ToastUtil;
import com.freak.wanandroid.util.ToolUtils;

/**
 * Created by 74099 on 2018/8/29.
 */

public class RegisterFragment extends BaseLifecycleFragment<RegisterViewModel> implements View.OnClickListener {
    private EditText mEdtRegisterRePw, mEdtRegisterPw, mEdtRegisterUser;
    private TextView mTvRegisterCommit;

    public static RegisterFragment newInstance() {
        RegisterFragment fragment = new RegisterFragment();
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_register;
    }

    @Override
    protected void initEventAndData(View view) {
        mEdtRegisterRePw = view.findViewById(R.id.edt_register_re_pw);
        mEdtRegisterPw = view.findViewById(R.id.edt_register_pw);
        mEdtRegisterUser = view.findViewById(R.id.edt_register_user);
        mTvRegisterCommit = view.findViewById(R.id.tv_register_commit);
        mTvRegisterCommit.setOnClickListener(this);
    }

    @Override
    protected void onResumeLoadData() {

    }

    private String getRegisterRePw() {
        return mEdtRegisterRePw.getText().toString().trim();
    }

    private String getRegisterPw() {
        return mEdtRegisterPw.getText().toString().trim();
    }

    private String getRegisterUserName() {
        return mEdtRegisterUser.getText().toString().trim();
    }

    public void doRegisterSuccess(LoginBean loginBean) {
        ToastUtil.showShort(getActivity(), getString(R.string.RegisterFragment_success));
        LoginActivity loginAct = (LoginActivity) getActivity();
        loginAct.doIntent();
        mEdtRegisterRePw.setText("");
        mEdtRegisterPw.setText("");
        mEdtRegisterUser.setText("");
    }

    public void doRegisterError(String msg) {
        ToastUtil.showShort(getActivity(), msg);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_register_commit) {
            if (ToolUtils.isNull(getRegisterUserName())) {
                ToastUtil.showShort(getActivity(), getString(R.string.login_user_hint));
            } else if (ToolUtils.isNull(getRegisterPw())) {
                ToastUtil.showShort(getActivity(), "请输入密码");
            } else if (ToolUtils.isNull(getRegisterRePw())) {
                ToastUtil.showShort(getActivity(), "请再次输入密码");
            } else if (!getRegisterPw().equals(getRegisterRePw())) {
                ToastUtil.showShort(getActivity(), "两次输入密码不一致");
            } else {
                mViewModel.doRegister(getRegisterUserName(), getRegisterPw(), getRegisterRePw());
            }
        }
    }

    @Override
    protected void dataObserver() {
        mViewModel.mDoRegister.observe(this, this::doRegisterSuccess);
        mViewModel.mDoRegisterError.observe(this, this::doRegisterError);
    }
}
